<?php


namespace app\controller;

use app\BaseController;
use app\model\UserModel;
class Login
{
    public function index()
    {
        return view();
    }

    public function login()
    {
        $name = $_GET['username'] ?? "";
        $pass = $_GET['password'] ?? "";
        if ($name == "" || $pass == "") {
            return json(['code' => 0, 'msg' => '信息不完整']);
        }

        $result = UserModel::where("username", $name)
            ->where("password", $pass)
            ->select();
        if (count($result) > 0) {
            session("username", $result[0]['username']);
            return json(['code' => 1, 'msg' => '登录成功']);
        } else {
            return json(['code' => 0, 'msg' => '用户名密码错误']);
        }

    }

    public function logout()
    {
        session('username', null);
        return redirect(url('login/index'));
    }
}